package grobit.torrent

import grobit.bencode.Bencoder

class TorrentBuilderTest extends GroovyTestCase {

    void testFileBuild() {
        def builder = new TorrentBuilder()
        def result = builder.torrent(
                'announce': 'http://localhost:8080/tracker.txt',
                'announce-list': ['http://localhost:1080', 'http://localhost:8080'],
                'creation date': new Date(113, 1, 27),
                'comment': 'Groovy',
                'created by': 'BarrySW19',
                'encoding': 'ISO-8859-1'
        ) {
            info(
                    'piece length': 32768,
                    'pieces': '0123456789012345678901234567890123456789',
                    'private': 1,
                    'name': 'groovy.zip',
                    'length': 49152,
                    'md5sum': '01234567890123456789012345678901'
            )
        }
        def bencoded = Bencoder.encode(result)

        assert bencoded == 'd8:announce33:http://localhost:8080/tracker.txt13:announce-listl' +
                '21:http://localhost:108021:http://localhost:8080e13:creation datei1361923200e7:comment6:Groovy' +
                '10:created by9:BarrySW198:encoding10:ISO-8859-14:infod12:piece lengthi32768e6:pieces' +
                '40:01234567890123456789012345678901234567897:privatei1e4:name10:groovy.zip6:lengthi49152e' +
                '6:md5sum32:01234567890123456789012345678901ee'

        new File('c:/Users/Barry/x.torrent').write(bencoded)

        MetaInfo metaInfo = MetaInfo.createFromMap(result)
        println metaInfo
    }
}
